﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KPIS.GERP.SERVICE.Model;

namespace KPIS.GERP.WEB.GAMS.Controllers
{
    public class ApprovalTaskController : Controller
	{
		private AbsenceApprovalWCF.AbsenceApprovalWCFClient absenceApprovalWCFClient = new AbsenceApprovalWCF.AbsenceApprovalWCFClient();
		private AbsenceApprovalWF.ServiceClient absenceApprovalWFClient = new AbsenceApprovalWF.ServiceClient();
        //private TempService.TempServiceClient tempServiceClient = new TempService.TempServiceClient();
		public static Guid DaemonWorkflowUniqueId;

		public ActionResult Index()
		{
			List<approval_task> obj;
			obj = (from t in absenceApprovalWCFClient.GetTasks()
					   select t).OrderByDescending(t => t.ApprovalTaskSeq).ToList();
			 
			return View(obj);
		}

		public ActionResult TakeAction(Guid wfInstantId, string actionCode, string messageTitle, string messageReasons, string backto = "")
		{
			try
			{
				absenceApprovalWFClient.ApproveAbsence(wfInstantId,
												 ApprovalUtil.TaskActionFromString(actionCode),
                                                 Class.GimsServiceData.CurrentEmployeeOldSeq, messageReasons, messageTitle);
				 
			}
			catch (Exception ex)
			{
				throw ex;
			}
			//if (backto == "Absence")
			//	return RedirectToAction("Index", "Absence");

			//if (backto == "SalaryPromote")
			//	return RedirectToAction("Index", "SalaryPromote");

			return RedirectToAction("Index");
		}

		public ActionResult Details(int approvalTaskSeq)
		{
			using (var serviceClient = new AbsenceApprovalWCF.AbsenceApprovalWCFClient())
			{
				var obj = (from t in serviceClient.GetTasks()
						   where t.ApprovalTaskSeq == approvalTaskSeq
						   select t).OrderByDescending(t => t.ApprovalTaskSeq).FirstOrDefault();

				return View(obj);
			}
		}

    }
}
